﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename.UC
{
    public class UcLinkLabel: LinkLabel
    {
        private class LinkClick
        {
            public string Name { get; set; }
            public bool IsClick { get; set; }
        }
        private EventHandler<UcLinkLabelEventArgs> _clickHandler = null;
        //private List<LinkClick> _linkClick=new List<LinkClick>();
        private bool _isClicked = false;

        public bool IsClicked { get => _isClicked; set => _isClicked = value; }

        public UcLinkLabel() 
        {
            AutoSize = false;
            BackColor = Color.Transparent;
            TextAlign= ContentAlignment.BottomRight;
            ImageAlign = ContentAlignment.TopCenter;
            Size = new Size(100, 70);
            Margin = new Padding(0, 0, 0, 0);
            
            LinkBehavior = LinkBehavior.NeverUnderline;
            MouseEnter += UcLinkLabel_MouseEnter;
            MouseLeave += UcLinkLabel_MouseLeave;
            Click += UcLinkLabel_Click;
        }

        private void UcLinkLabel_Click(object sender, EventArgs e)
        {
            LinkLabel link=sender as LinkLabel;
            switch (link.Text)
            {
                case "文件重命名":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽文件到此区域", Type = CatalogType.File });
                    break;
                case "图片重命名":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽图片视频文件到此区域", Type = CatalogType.File });
                    break;
                case "文件夹重命名":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽文件夹到此区域", Type = CatalogType.Directory });
                    break;
                case "文件时间属性":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽文件到此区域", Type = CatalogType.File });
                    break;
                case "文件夹时间属性":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽文件夹到此区域", Type = CatalogType.Directory });
                    break;
                case "提取文件名称":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽文件到此区域", Type = CatalogType.File });
                    break;
                case "提取文件夹名称":
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "点击添加 / 拖拽文件夹到此区域", Type = CatalogType.Directory });
                    break;
                default:
                    OnClickLinkLabel(new UcLinkLabelEventArgs() { Text = "" });
                    break;
            }
        }
        [Browsable(true)]
        [Category("行为")]
        [Description("当链接被单击时发生")]
        public event EventHandler<UcLinkLabelEventArgs> ClickLinkLabel
        {
            add { this._clickHandler += value; }
            remove { this._clickHandler -= value; }
        }
        protected virtual void OnClickLinkLabel(UcLinkLabelEventArgs e)
        {
            var temp = System.Threading.Volatile.Read(ref _clickHandler);
            if (temp != null)
            {
                temp(this,e);
            }       
        }
        public void SetMouseEnterIcon()
        {
            Image img = global::Rename.Properties.Resources.ResourceManager.GetObject($"{Text}22") as Image;
            if (img != null)
            {
                this.Image = img;
            }
            LinkColor = Color.FromArgb(255, 120, 120);//#FF7878
        }
        public void SetMouseLeaveIcon()
        {
            Image img = global::Rename.Properties.Resources.ResourceManager.GetObject($"{Text}11") as Image;
            if (img != null)
            {
                this.Image = img;
            }
            LinkColor = Color.FromArgb(19, 34, 122); //#13227A
        }

        private void UcLinkLabel_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
            string text = string.IsNullOrEmpty(Text) == true?Tag?.ToString():Text;
            Image img = global::Rename.Properties.Resources.ResourceManager.GetObject($"{text}22") as Image;
            if(img!=null)
            {
                Image=img;
            }
            LinkColor = Color.FromArgb(255,120,120);//#FF7878
        }

        private void UcLinkLabel_MouseLeave(object sender, EventArgs e)
        {
            if (_isClicked==true) return;
            string text = string.IsNullOrEmpty(Text) == true ? Tag?.ToString() : Text;
            Image img = global::Rename.Properties.Resources.ResourceManager.GetObject($"{text}11") as Image;
            if (img != null)
            {
                Image = img;
            }
            LinkColor = Color.FromArgb(19,34,122); //#13227A
        }
    }
}
